package ru.elifantiev.cityrouter;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import ru.elifantiev.cityrouter.infrastructure.map.*;

public class MainActivity extends Activity implements MapWrapper.OnClickListener {

    private boolean isMapReady;
    private MapControlsWrapper mapControlsWrapper;
    private MapWrapper map;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MapFactory.registerImplementationLayout("Yandex", R.layout.map_yandex);
        map = MapFactory.build(
                new ViewCache(this),
                this,
                (FrameLayout) findViewById(R.id.mapContainer));
        map.setOnMapReadyListener(new MapWrapper.OnMapReadyListener() {
            public void mapReady(MapWrapper map) {
                isMapReady = true;
                Location best = LocationUtility.getLastLocation(MainActivity.this);
                if (best != null)
                    map.setCenter(LocationUtility.pointFromLocation(best));
                else
                    map.setCenter(LocationUtility.getDefaultPoint());
                map.showMyLocation(true);
                map.setMode(MapWrapper.MAP_MODE_VECTOR);
                map.setZoom(16);
            }
        });
        mapControlsWrapper = new MapControlsWrapper(MainActivity.this, map);

        findViewById(R.id.btnCalcRoute).setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, getString(R.string.clickAMap), Toast.LENGTH_LONG).show();
                map.setOnClickHandler(MainActivity.this);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && requestCode == MapControlsWrapper.REQUEST_SELECT_ADDRESS) {
            if (data.getAction().equals(GeoSearchActivity.SEARCH_RESULT_GEOCODER_RESPONSE)) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                    mapControlsWrapper.showSelectedLocation(extras);
                }
            }
        }
    }

    @Override
    protected void onResume() {
        map.resume();
        mapControlsWrapper.resume();
        super.onResume();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void onPause() {
        mapControlsWrapper.pause();
        map.pause();
        super.onPause();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void onDestroy() {
        mapControlsWrapper.destroy();
        map.destroy();
        mapControlsWrapper = null;
        map = null;
        super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.
    }

    public void onClick(MapWrapper map, GeoPoint clickPoint) {
        map.setOnClickHandler(null);
        GeoPoint
                from = LocationUtility.pointFromLocation(LocationUtility.getLastLocation(this));

        Intent call = new Intent(this, RoutesDisplayActivity.class);
        call.putExtra("from_lat", String.valueOf(from.getLatitudeE6() / 1E6));
        call.putExtra("from_lon", String.valueOf(from.getLongitudeE6() / 1E6));

        call.putExtra("to_lat", String.valueOf(clickPoint.getLatitudeE6() / 1E6));
        call.putExtra("to_lon", String.valueOf(clickPoint.getLongitudeE6() / 1E6));

        startActivity(call);
    }
}
